//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SFWK.Core.CompositionAttribute;
using SFWK.Core.DTO;
using SFWK.Core.Linq;

namespace SFWK.Services
{
    public class CompositService : BaseService
    {

        #region Change Tracker Handling
        protected ObjectsAddedToCollectionProperties getObjectsAddedToCollection(ObjectChangeTracker source)
        {
            ObjectsAddedToCollectionProperties res = new ObjectsAddedToCollectionProperties();
            foreach (var val in source.ObjectsAddedToCollectionProperties)
                res.Add(val.Key, val.Value);
            return res;
        }
        protected OriginalValuesDictionary getObjectsUpdatedinCollection(ObjectChangeTracker source)
        {
            OriginalValuesDictionary res = new OriginalValuesDictionary();
            foreach (var val in source.OriginalValues)
                res.Add(val.Key, val.Value);
            return res;
        }

        protected void setChangeTrackerValues(ObjectsAddedToCollectionProperties objectAdded)
        {
            foreach (var key in objectAdded.Keys)
                foreach (IObjectWithChangeTracker entity in objectAdded[key])
                    entity.ChangeTracker.State = ObjectState.Added;
        }

        protected void setChangeTrackerValues(OriginalValuesDictionary objectUpdated)
        {
            foreach (var key in objectUpdated.Keys)
                ((IObjectWithChangeTracker)objectUpdated[key]).ChangeTracker.State = ObjectState.Modified;
        }



        #endregion Change Tracker Handling

        #region Graph handling
        /// <summary>
        /// Decompose a list of graph anr sort depending on composition objects
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="graphs"></param>
        /// <returns></returns>
        protected Dictionary<string, List<string>> DecomposeGraphs(List<string> graphs)
        {
            Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
            foreach (string graph in graphs)
            {
                string entityName = graph.Substring(0, graph.IndexOf("."));
                string graphValue = graph.Substring(graph.IndexOf(".") + 1);
                if (!result.Keys.Contains(entityName))
                {
                    result.Add(entityName, new List<string>());
                }
                result[entityName].Add(graphValue);
            }

            return result;
        }


        //protected Dictionary<string, List<string>> DecomposeGraphs<T>(List<ControlledGraph<T>> graphs)
        //{
        //    Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
        //    foreach (var controlledGraph in graphs)
        //    {
        //        string graph = controlledGraph.Graph;
        //        string entityName = graph.Substring(0, graph.IndexOf("."));
        //        string graphValue = graph.Substring(graph.IndexOf(".") + 1);
        //        if (!result.Keys.Contains(entityName))
        //        {
        //            result.Add(entityName, new List<string>());
        //        }
        //        result[entityName].Add(graphValue);
        //    }

        //    return result;
        //}




        #endregion Graph handling

    }
}
